package org.lzfto.flink.demo.config;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.ObjectUtils;
import org.lzfto.flink.demo.business.flinkTest.base.BasicTable;
import org.lzfto.flink.demo.business.flinkTest.base.db.BasicJdbcTable;
import org.lzfto.flink.demo.business.flinkTest.base.db.CustomJdbcTable;
import org.lzfto.flink.demo.business.flinkTest.base.db.StandardJdbcTable;
import org.lzfto.flink.demo.business.flinkTest.base.file.ExcelSourceTable;
import org.lzfto.flink.demo.utils.SpringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.io.Resource;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;

public class AppConfiguration {
	
	private static final  Logger LOGGER=LoggerFactory.getLogger(AppConfiguration.class);
	private static  AppConfiguration conf;	
	
	private List<BasicTable> tables;
	/**
	 * 环境名称。各个配置通常根据此选择对应的配置
	 * </br> 等于*表示符合各种情况
	 */
	private String env;
	private List<Dir> fileDirectory;

	private String artemisHost;
	private String artemisUserName;
	private String artemisPassword;
	private String dbUserName;
	private String dbPassword;
	
	private Boolean isDatasourceRegistered;
	private Boolean isArtemisRegistered;
	
	private MyAppProperties appProp;

	public MyAppProperties getAppProp() {
		return appProp;
	}
	public Boolean getIsDatasourceRegistered() {
		return isDatasourceRegistered;
	}
	public Boolean getIsArtemisRegistered() {
		return isArtemisRegistered;
	}
	
	public void setIsDatasourceRegistered(Boolean isDatasourceRegistered) {
		this.isDatasourceRegistered = isDatasourceRegistered;
	}
	public void setIsArtemisRegistered(Boolean isArtemisRegistered) {
		this.isArtemisRegistered = isArtemisRegistered;
	}
	public String getArtemisHost() {
		return artemisHost;
	}
	public void setArtemisHost(String artemisHost) {
		this.artemisHost = artemisHost;
	}
	public String getArtemisUserName() {
		return artemisUserName;
	}
	public void setArtemisUserName(String artemisUserName) {
		this.artemisUserName = artemisUserName;
	}
	public String getArtemisPassword() {
		return artemisPassword;
	}
	public void setArtemisPassword(String artemisPassword) {
		this.artemisPassword = artemisPassword;
	}
	public String getDbUserName() {
		return dbUserName;
	}
	public void setDbUserName(String dbUserName) {
		this.dbUserName = dbUserName;
	}
	public String getDbPassword() {
		return dbPassword;
	}
	public void setDbPassword(String dbPassword) {
		this.dbPassword = dbPassword;
	}
	public List<BasicTable> getTables() {
		return tables;
	}
	public String getEnv() {
		return env;
	}
	public List<Dir> getFileDirectory() {
		return fileDirectory;
	}
	
	/**
	 * 获取当前环境的文件路径
	 * @return
	 */
	public String getEnvDirectory() {
		if (ObjectUtils.isEmpty(fileDirectory)){
			return "";
		}
		for(Dir item:fileDirectory) {
			if (item.env().equals(this.env)) {
				return item.directory();
			}
		}
		return "";
	}

	public static void init(MyAppProperties myAppConfig) {
		LOGGER.info("启用配置:{}",myAppConfig.getTableConfig());		
		AppConfiguration config = new AppConfiguration();
		config.appProp =myAppConfig;
		config.setIsArtemisRegistered(false);
		config.setIsDatasourceRegistered(false);
		List<BasicTable> tables = new ArrayList<>();
		String env=null;
		String resourceConfig="classpath:"+myAppConfig.getTableConfig();
		Resource resource = SpringUtils.getApplicationContext().getResource(resourceConfig);
		List<Dir> fileDirectory=null;
		try (InputStream inputStream = resource.getInputStream()) {
			if (inputStream != null) {
				// 将 InputStream 转换为字符串
				String jsonContent = new String(inputStream.readAllBytes());
				// 使用 Fastjson 2 解析 JSON 字符串为 JSONObject
				JSONObject jsonObject = JSON.parseObject(jsonContent);
				// 访问 JSON 对象的字段
				env = jsonObject.getString("env");
				System.out.println("当前环境:"+env);
				JSONArray fileJa=jsonObject.getJSONArray("fileDirectory");
				if (fileJa!=null) {
					fileDirectory=fileJa.toList(Dir.class);
				}
				//构建文件信息
				
				//构建表
				JSONArray ja = jsonObject.getJSONArray("tables");

				if (ja != null && ja.size() > 0) {
					int len = ja.size();
					for (int i = 0; i < len; i++) {
						JSONObject item = ja.getJSONObject(i);
						String type=item.getString("type");
						if (type.equals(BasicTable.TABLE_TYPE_JDBC)) {
							BasicJdbcTable table = null;
							if (item.getString("idColCode") != null) {
								table = JSON.to(CustomJdbcTable.class, item);								
							} else {
								table = JSON.to(StandardJdbcTable.class, item);								
							}
							tables.add(table);
							System.out.println("找到表(JDBC)-env=" + table.getEnv() + ",direction=" + table.getDirection() + ",name="
									+ table.getTableName());
						}
						else if (type.equals(BasicTable.TABLE_TYPE_EXCEL)){
							ExcelSourceTable table=JSON.to(ExcelSourceTable.class, item);
							table.setExtension("xlsx");
							table.setExtensionHyphen("-"); 
							tables.add(table);
							System.out.println("找到表(EXCEL)-env=" + table.getEnv()  + ",name="
									+ table.getTableName());
						}
					}
				}

			} else {
				System.err.println("config.json not found in resources!");
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		config.env=env;
		config.fileDirectory=fileDirectory;
		config.tables=tables;
		conf=config;
	}
	
	public static AppConfiguration getConf() {
		return conf;
	}
	
	record Dir(String env,String directory) {}

}
